<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="培训标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入培训标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- 培训人员 从userinfo中获取 点击选择 -->
      <el-form-item label="培训人员" prop="userName">
        <div @click="handleSelctUser">
          <el-input
            v-model="queryParams.userName"
            placeholder="请选中人员"
            clearable
            @clear="handleClear"
          />
        </div>
      </el-form-item>
      <el-form-item label="培训日期" prop="trainingTime">
        <el-date-picker clearable
                        v-model="queryParams.trainingTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择培训日期">
        </el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="培训方式" prop="trainingMethod">-->
      <!--        <el-select v-model="queryParams.trainingMethod" placeholder="请选择培训方式" clearable>-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.training_method"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="培训层级" prop="trainingLevel">-->
      <!--        <el-select v-model="queryParams.trainingLevel" placeholder="请选择培训层级" clearable>-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.training_level"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="考核结果" prop="result">-->
      <!--        <el-select v-model="queryParams.result" placeholder="请选择考核结果" clearable>-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.assessment_result"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="培训类型" prop="trainingType">
        <el-select v-model="queryParams.trainingType" placeholder="请选择培训类型" clearable>
          <el-option
            v-for="dict in dict.type.training_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['exam:training:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['exam:training:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['exam:training:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['exam:training:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="trainingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="主键，培训记录ID" align="center" prop="id" />-->
      <el-table-column label="培训标题" align="center" prop="title"/>
      <el-table-column label="人员" align="center" prop="userName"/>
      <!--      <el-table-column label="人员" align="center">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ getUserName(scope.row.userId) }}</span> &lt;!&ndash; 使用计算属性或方法获得用户名 &ndash;&gt;-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="培训日期" align="center" prop="trainingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.trainingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="培训内容" align="center" prop="trainingContent" />-->
      <el-table-column label="培训内容" align="center" prop="trainingContent">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="removeHtmlTags(scope.row.trainingContent)" placement="top">
            <div style="max-width: 200px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
              <!-- 正常显示时使用 removeHtmlTags 去除 HTML 标签 -->
              {{ removeHtmlTags(scope.row.trainingContent) }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column type="expand">
        <template slot-scope="props">
          <div style="padding: 10px; max-width: 500px; word-wrap: break-word;">
            <strong>详细内容:</strong>
            <p><span v-html="props.row.trainingContent"></span></p>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="培训类型" align="center" prop="trainingType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.training_type" :value="scope.row.trainingType"/>
        </template>
      </el-table-column>
      <el-table-column label="培训方式" align="center" prop="trainingMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.training_method" :value="scope.row.trainingMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="培训层级" align="center" prop="trainingLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.training_level" :value="scope.row.trainingLevel"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="文件路径" align="center" prop="filePath" />-->
      <!--      <el-table-column label="考核结果" align="center" prop="result">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.assessment_result" :value="scope.row.result"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['exam:training:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exam:training:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改个人安全教育培训档案，用于记录个人的安全教育培训信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="培训标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入培训标题"/>
        </el-form-item>
        <el-form-item label="培训日期" prop="trainingTime">
          <el-date-picker clearable
                          v-model="form.trainingTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择培训日期">
          </el-date-picker>
        </el-form-item>
        <!-- 新增培训人员(点击输入框弹出用户列表弹窗，../training/userinfo.vue ,选择后带出用户ID)-->
        <el-form-item label="培训人员" prop="userId">
          <div @click="handleSelctUser">
            <el-input v-model="form.userName" placeholder="请选中人员"/>
          </div>
        </el-form-item>
        <el-form-item label="培训内容">
          <editor v-model="form.trainingContent" :min-height="192"/>
        </el-form-item>
        <!--zj: 新增培训类型、培训方式、培训层级、考核结果-->
        <el-form-item label="培训类型" prop="trainingType">
          <el-select v-model="form.trainingType" placeholder="请选择培训类型">
            <el-option
              v-for="dict in dict.type.training_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="培训方式" prop="trainingMethod">
          <el-select v-model="form.trainingMethod" placeholder="请选择培训方式">
            <el-option
              v-for="dict in dict.type.training_method"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="培训层级" prop="trainingLevel">
          <el-select v-model="form.trainingLevel" placeholder="请选择培训层级">
            <el-option
              v-for="dict in dict.type.training_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="文件路径" prop="filePath">-->
        <!--          <el-input v-model="form.filePath" type="textarea" placeholder="请输入内容" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="考核结果" prop="result">-->
        <!--          <el-select v-model="form.result" placeholder="请选择考核结果">-->
        <!--            <el-option-->
        <!--              v-for="dict in dict.type.assessment_result"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="parseInt(dict.value)"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <User_Detail ref="User_Detail" @confirm="handleConfirm"/>
  </div>
</template>

<script>
import {listTraining, getTraining, delTraining, addTraining, updateTraining} from "@/api/exam/training";
import User_Detail from "./userinfo.vue";

export default {
  name: "Training",
  components: {
    User_Detail
  },
  dicts: ['training_level', 'training_method', 'assessment_result', 'training_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 个人安全教育培训档案，用于记录个人的安全教育培训信息表格数据
      trainingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        userId: null,
        userName: null,
        trainingTime: null,
        trainingMethod: null,
        trainingLevel: null,
        result: null,
        trainingType: null,
      },
      // 表单参数
      form: {
        result: 1
      },
      // 表单校验
      rules: {
        title: [
          {required: true, message: "培训标题不能为空", trigger: "blur"}
        ],
        userId: [
          {required: true, message: "人员ID，关联人员表主键不能为空", trigger: "change"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },

  methods: {
    /** 查询个人安全教育培训档案，用于记录个人的安全教育培训信息列表 */
    getList() {
      this.loading = true;
      listTraining(this.queryParams).then(response => {
        this.trainingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 去除 HTML 标签，返回纯文本
    removeHtmlTags(text) {
      if (!text) return '';
      return text.replace(/<[^>]+>/g, ''); // 正则去除所有 HTML 标签
    },
    handleConfirm(userId, userName) {
      this.form.userId = userId;  // 将子组件传递的 userId 更新到父组件的 form.userId
      this.form.userName = userName;  // 将子组件传递的 userName 更新到父组件的 form.userName
      this.queryParams.userId = userId;  // 将子组件传递的 userId 更新到父组件的 queryParams.userId
      this.queryParams.userName = userName;  // 将子组件传递的 userName 更新到父组件的 queryParams.userName
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        userId: null,
        userName: null,
        trainingTime: null,
        trainingContent: null,
        trainingMethod: null,
        trainingLevel: null,
        filePath: null,
        result: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        trainingType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleSelctUser() { // 选择用户
      this.$refs.User_Detail.showDialog()
    },
    handleClear(event) {
      console.log('event',event)
      // 如果需要做其他处理，可以在这里进行，确保不触发handleSelctUser
      this.dialogVisible = false;
      this.queryParams.userId = null;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加个人安全教育培训档案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTraining(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改个人安全教育培训档案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTraining(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTraining(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除个人安全教育培训档案，编号为"' + ids + '"的数据项？').then(function () {
        return delTraining(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('exam/training/export', {
        ...this.queryParams
      }, `training_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
